﻿/*
   RxInputManager
   Copyright 2011 Jordan Neumeyer

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   File: RxGeneralExt.cs 
 */
using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;

namespace RxInputManager.Extensions
{
    public static class RxGeneralExt
    {
        public static IObservable<T> DoubleTap<T>(this IObservable<T> src, int greatestTimeBetween = RxInputManager.Constants.DoubleTapDefault)
        {
            return src.BufferWithTimeAndCount(TimeSpan.FromMilliseconds(greatestTimeBetween), 2);
        }

        public static IObservable<T> BufferWithTimeAndCount<T>(this IObservable<T> src, TimeSpan t, int Count)
        {
            return src.Timestamp()
                      .Buffer(Count)
                      .Where(s => s[s.Count - 1].Timestamp - s[0].Timestamp <= t)
                      .Select(arr => arr[arr.Count - 1].Value);
        }

        // http://social.msdn.microsoft.com/Forums/en-US/rx/thread/aa97da9e-0d16-4f6e-9c77-8b60145ae280
        // Credit goes to Dave in that thread.
        public static IObservable<TResult> Concat<TSource, TResult>(
                                    this IObservable<TSource> source,
                                    Func<TSource, IObservable<TResult>> selector)
        {

            return Observable.Defer(() =>
            {
                var queue = new BehaviorSubject<Unit>(new Unit());

                return source
                    .Zip(queue, (v, q) => v)
                    .SelectMany(v => selector(v)
                        .Do(_ => { }, () => queue.OnNext(new Unit()))
                    );
            });

        }

        // http://social.msdn.microsoft.com/Forums/en-US/rx/thread/aa97da9e-0d16-4f6e-9c77-8b60145ae280
        public static IObservable<T> DelayEach<T>(this IObservable<T> source, TimeSpan minDelay)
        {
            return source.Concat(x =>
                Observable.Empty<T>()
                .Delay(minDelay)
                .StartWith(x)
            );
        }

        public static IObservable<T> DelayEachUntil2<T>(this IObservable<T> source, IObservable<T> other, TimeSpan minDelay)
        {
            return source.Concat(x =>
                Observable.Return(x)

                          .Delay(minDelay)
                          .TakeUntil(other)
                //.TakeUntil(other)

            );
        }

        public static IObservable<T> DelayEachUntil2<T, U>(this IObservable<T> source, TimeSpan minDelay, IObservable<U> other)
        {
            return source.Concat(x =>
                Observable.Return(x)
                          .Delay(minDelay)
                          .TakeUntil(other)
            );
        }

        public static IObservable<T> DelayEachUntil<T>(this IObservable<T> source, IObservable<T> other, TimeSpan minDelay)
        {
            return source.TakeUntil(other)
                         .Take(1)
                         .Concat(source.Delay(minDelay).Take(1).Repeat());

        }

        public static IObservable<T> DelayEachUntil<T,U>(this IObservable<T> source, IObservable<U> other, TimeSpan minDelay)
        {
            return source.TakeUntil(other)
                         .Take(1)
                         .Concat(source.Delay(minDelay).Take(1).Repeat());

        }
    }

        //public static IObservable<T> DelayEachUntil<T>(this IObservable<T> source, TimeSpan minDelay, Func<T,IObservable<T>> func)
        //{
        //    //return source.Publish(xs =>
        //    //        xs.TakeUntil(other)
        //    //          .Take(1)
        //    //          .Concat(xs.Delay(minDelay).Take(1).Repeat())
        //    //        );
        //    return source
        //                 .Take(1)
        //                 .Concat(source.Delay(minDelay).Take(1).Repeat()).Concat(func);

        //}
}
